package com.study.Socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class SocketClient {
    public static void main(String[] args) throws Exception {
        Socket socket=new Socket("127.0.0.1",10086);
        OutputStream os=null;
        int num=0;
        Scanner scanner = new Scanner(System.in);
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is=null;
                try{
                    while(true){
                        is=socket.getInputStream();
                        int len = 0;
                        byte[] buf = new byte[1024];
                        while ((len=is.read(buf))!=-1)
                        { System.out.println("service saying: "+new String(buf,0,len));
                        }
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        }).start();
        while(true){
            //发送
            os = socket.getOutputStream();//字节输出流
            String msg=scanner.nextLine();
            os.write(msg.getBytes());
            os.flush();
        }

    }
}
